package com.mystudy.concurrent.designModel.observerModel.thread;

/**
 * TODO:
 *
 * @author acy
 * @since 2019/05/27 21:03
 */
public class ObservableRunnable implements Runnable{
	final protected LifeCycleListener listener;

	public ObservableRunnable(final LifeCycleListener listener) {
		this.listener = listener;
	}

	/**
	 * 线程运行状态
	 */
	public enum RunnableState{
		RUNNING,ERROR,DONE;
	}

	/**
	 * 线程运行事件
	 */
	public static class RunnableEvent{
		//线程状态
		private final RunnableState state;
		//运行的线程
		private final Thread thread;
		//错误原因
		private final Throwable throwable;

		public RunnableEvent(RunnableState state, Thread thread, Throwable throwable) {
			this.state = state;
			this.thread = thread;
			this.throwable = throwable;
		}
		public RunnableState getState() {
			return state;
		}

		public Thread getThread() {
			return thread;
		}

		public Throwable getThrowable() {
			return throwable;
		}

	}

	/**
	 * 线程状态改变的时候通知监听者
	 * @param runnableEvent 线程状态事件
	 */
	protected void notifyStateChanged(final RunnableEvent runnableEvent){
		listener.onStateChangeEvent(runnableEvent);
	}
	@Override
	public void run() {

	}
}
